package com.tom.demo.test;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;

import java.io.FileInputStream;

public class JavaFx03 extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("这是我的第一个JavaFx项目");

        FileInputStream input = new FileInputStream("1121.jpg");
        Image image = new Image(input);
        ImageView imageView = new ImageView(image);
        imageView.setFitWidth(100);
        imageView.setFitHeight(100);

        Group group = new Group(imageView);

        Scene scene = new Scene(group,600,600);

        stage.setScene(scene);

        stage.addEventHandler(KeyEvent.KEY_PRESSED, e->{

            if("W".equals(e.getCode().getName())){
                imageView.setLayoutY(imageView.getLayoutY()-3);
            }
            if("S".equals(e.getCode().getName())){
                imageView.setLayoutY(imageView.getLayoutY()+3);
            }
            if("A".equals(e.getCode().getName())){
                imageView.setLayoutX(imageView.getLayoutX()-3);
            }
            if("D".equals(e.getCode().getName())){
                imageView.setLayoutX(imageView.getLayoutX()+3);
            }
        });

        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}
